package com.xc.freeapp.account.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.common.ResponsePage;
import com.xc.freeapp.entity.Doctor;
import com.xc.freeapp.entity.Takemedic;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.TakemedicService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.DateUtils;
import com.xc.freeapp.util.TokenAnnotation;
import com.xc.freeapp.util.UploadUtil;

@Controller
@RequestMapping("/v2/takemedic")
@Api(value = "/v2/takemedic", description = "定时吃药")
public class TakeMedicControllerV2 extends AbstractController {
	@Autowired
	private TakemedicService takemedicservice;

	/**
	 * 获取定时吃药列表
	 * */
	@TokenAnnotation(required = true)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取定时吃药列表")
	public ResponsePage<List<Takemedic>> getTakeMedicList(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		log.info("获取id" + authInfo.getUserId() + "的定时吃药列表");
		PageBounds pageBounds = new PageBounds(page, limit);
		List<Takemedic> takemediclist = takemedicservice.gettakemediclist(
				authInfo.getUserId(), pageBounds);
		Paginator paginator = ((PageList) takemediclist).getPaginator();
		for (Takemedic takemedic : takemediclist) {
			if (takemedic.getTaketime() != null
					&& !"".equals(takemedic.getTaketime())) {
				List<String> taketimelist = JSONArray.parseArray(
						takemedic.getTaketime(), String.class);
				List<Integer> taketimeintlist = new ArrayList<Integer>();
				for (String string : taketimelist) {
					String[] a = string.split(":");
					taketimeintlist.add(Integer.valueOf(a[0]) * 3600 * 1000
							+ Integer.valueOf(a[1]) * 60 * 1000);
				}
				takemedic.setTaketimelist(taketimeintlist);

			}

			takemedic.setPicslist(UploadUtil.pathstoList(takemedic.getPics()));
		}
		return new ResponsePage<List<Takemedic>>(takemediclist, paginator);
	}

	/**
	 * 定时吃药保存
	 * 
	 * @param datamap
	 * @return
	 * @throws BaseException
	 */
	@TokenAnnotation(required = true)
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "保存定时吃药")
	public Takemedic saveTakeMedic(@RequestBody @Valid Takemedic takemedic)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		takemedic.setUserid(authInfo.getUserIntId());
		takemedic.setCreatetime(DateUtils.getDate());
		takemedic.setUpdatetime(DateUtils.getDate());
		takemedic.setCreateby(authInfo.getUserIntId());
		takemedic.setUpdateby(authInfo.getUserIntId());
		takemedic.setDelflag('t');
		takemedic.setPics(UploadUtil.uploads(takemedic.getPicslist(),authInfo.getAppIntId()));
		List times = new ArrayList();
		if (takemedic.getPicslist() != null&& !"".equals(takemedic.getPicslist())) {
			for (Integer time : takemedic.getTaketimelist()) {
				Date date = new Date(time);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.HOUR_OF_DAY, -8);
				times.add(sdf.format(calendar.getTime()));

			}
			takemedic.setTaketime(JSONArray.toJSONString(times));
		}

		takemedicservice.inserttakemedic(takemedic);
		Takemedic takemedics = takemedicservice.gettakemedic(takemedic.getId());
		takemedics.setPicslist(UploadUtil.pathstoList(takemedics.getPics()));
		return takemedics;
	}

	/**
	 * 更新定时吃药
	 * 
	 * @param datamap
	 * @return
	 * @throws BaseException
	 */
	@TokenAnnotation(required = true)
	@RequestMapping(value = "", method = RequestMethod.PUT)
	@ResponseBody
	@ApiOperation(value = "更新定时吃药")
	public Takemedic updateTakeMedic(@RequestBody Takemedic takemedic)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		takemedic.setUserid(authInfo.getUserIntId());
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		takemedic.setPics(UploadUtil.uploads(takemedic.getPicslist(),authInfo.getAppIntId()));
		List times = new ArrayList();
		if (takemedic.getPicslist() != null&&! "".equals(takemedic.getPicslist())) {
			for (Integer time : takemedic.getTaketimelist()) {
				Date date = new Date(time);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.HOUR_OF_DAY, -8);
				times.add(sdf.format(calendar.getTime()));

			}
		}
		takemedic.setTaketime(JSONArray.toJSONString(times));
		takemedicservice.updatetakemedic(takemedic);
		Takemedic takemedics = takemedicservice.gettakemedic(takemedic.getId());
		takemedics.setPicslist(UploadUtil.pathstoList(takemedics.getPics()));
		return takemedics;
	}

	/**
	 * 删除定时吃药
	 * 
	 * @throws BaseException
	 */
	// @TokenAnnotation(required=true)
	// @RequestMapping(value = "", method = RequestMethod.DELETE)
	// @ResponseBody
	// @ApiOperation(value = "删除定时吃药")
	public ResponseBean delTakeMedic(@RequestParam("id") String id)
			throws BaseException {
		takemedicservice.deletetakemedic(id);
		return ResponseBean.getSuccess();
	}
}
